export function arrayToTree(list, parentId = 0) {
  return list
    .filter((item) => item.parentId === parentId)
    .map((item) => ({
      ...item,
      children: arrayToTree(list, item.menuId),
    }));
}
//只返回选中的菜单
export function arrayToTreeHas(list, parentId = 0 ,choose) {
  return list
    .filter((item) => item.parentId === parentId)
    .map((item) => {
      choose.map(val=> {
        if(val == item.menuId){
          return {
            ...item,
            children: arrayToTree(list, item.menuId),
          }
        }
      })
    });
}

export function array2Tree(data, pid = 0) {
  let res = [];
  data.forEach((item) => {
    if (item.parentId === pid) {
      let itemChildren = array2Tree(data, item.menuId);
      if (itemChildren.length) item.children = itemChildren;
      res.push(item);
    }
  });
  return res;
}

export function menuToTree(list, parentId = 0) {
  return list
    .filter((item) => (item.parentId === parentId && item.type !==2))
    .map((item) => ({
      ...item,
      children: menuToTree(list, item.menuId),
    }));
}

export function arrayToObject(array) {
  var obj = {};
  for (let i = 0; i < array.length; i++) {
    let prop = Object.keys(array[i])[0];
    obj[prop] = array[i][prop];
  }
  return obj;
}
export function treeToArray(list, newArr = []) {
  list.forEach((item) => {
    const { children } = item;
    if (children && children.length) {
      treeToArray(children, newArr);
      // delete item.children
    }
    newArr.push(item);
  });
  return newArr;
}

// 去重
export function unique(arr) {
  var arr1 = [];
  for (var i = 0, len = arr.length; i < len; i++) {
    if (!arr1.includes(arr[i])) {
      // 检索arr1中是否含有arr中的值
      arr1.push(arr[i]);
    }
  }
  return arr1;
}

//搜索i条件处理
export function objectFilter(obj) {
  let object = {};
  let keys = Object.keys(obj).filter(
    (item) => !(obj[item] === "" || obj[item] === undefined)
  );
  keys.forEach((i) => (object[i] = obj[i]));
  return object;
}

/**
 * 百度地图---->腾讯地图
 * @param double $lat 纬度
 * @param double $lng 经度
 * @return array();
 */
function bdMapToTxMap(lng, lat) {
  let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  let x = lng - 0.0065;
  let y = lat - 0.006;
  let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  let lngs = z * Math.cos(theta);
  let lats = z * Math.sin(theta);
  let point = {
    lng: lngs,
    lat: lats        
    }   
  return lngs
}
export function numToStringTime(minutes) {
  let time = minutes ? parseInt(minutes/60) : 0
  let hour = Math.floor(time/60)
  let surMin = time - (Math.floor(time/60)*60)
  if(hour){
    return hour +'小时'+ surMin+ "分钟"
  }else {
    return  surMin+ "分钟"
  }
  
}